package com.itheima.supermarket.manage.controller;

import com.itheima.supermarket.manage.domain.Fruits;
import com.itheima.supermarket.manage.domain.Managers;
import com.itheima.supermarket.manage.servicer.FruitServicer;
import com.itheima.supermarket.manage.servicer.ManagerServicer;

import java.util.ArrayList;
import java.util.Scanner;

public class ManagerController {

    //键盘录入对象
    private Scanner sc = new Scanner(System.in);
    //ManagerServicer对象
    private ManagerServicer ms = new ManagerServicer();
    //CustomerController对象
    private CustomerController cc = new CustomerController();
    //FruitServicer对象
    private FruitServicer fs = new FruitServicer();

    //账号登陆，注册界面
    public void managerController() {
        while (true) {
            System.out.println("请选择：1.登陆账号    2.注册账号    3.退出");

            String choice = sc.next();

            switch (choice) {
                case "1":
                    //System.out.println("登陆账号");
                    login();//不完善
                    break;
                case "2":
                    //System.out.println("注册账号");
                    register();
                    break;
                case "3":
                    System.out.println("退出");
                    return;
                case "4":
                    System.out.println("您的输入有误，请重新输入！");
                    break;
            }
        }
    }

    //管理水果库界面
    public void manage() {
        while (true) {
            System.out.println("====================管理员====================");
            System.out.println("请选择：1.查看水果   2.添加水果   3.修改水果   4.删除水果   5.退出");
            String choice = sc.next();

            switch (choice) {
                case "1":
                    //System.out.println("查看水果");
                    cc.checkFruits();
                    break;
                case "2":
                    //System.out.println("添加水果");
                    addFruits();
                    break;
                case "3":
                    //System.out.println("修改水果");
                    updateFruits();
                    break;
                case "4":
                    //System.out.println("删除水果");
                    deleteFruits();
                    break;
                case "5":
                    System.out.println("退出");
                    return;
                case "6":
                    System.out.println("您的输入有误，请重新输入！");
                    break;
            }
        }
    }

    //修改水果
    private void updateFruits() {
        System.out.println("请输入水果编号：");
        String number = sc.next();
        boolean exsits = fs.isExsits(number);
        if (!exsits) {
            System.out.println("该水果不存在，请重新输入！");
        } else {
            Fruits fruit = newFruits(number);
            fs.updateFruits(fruit, number);
            System.out.println("修改成功！");
        }
    }

    //删除水果
    private void deleteFruits() {
        System.out.println("请输入水果编号：");
        String number = sc.next();
        boolean exsits = fs.isExsits(number);
        if (exsits) {
            fs.deleteFruits(number);
            System.out.println("删除成功！");
        } else {
            System.out.println("水果编号不存在，请重新输入！");
        }
    }

    //添加水果
    private void addFruits() {
        System.out.println("请输入要添加的水果编号：");
        String number = sc.next();
        boolean exsits = fs.isExsits(number);
        if (exsits) {
            System.out.println("该水果已存在！");
        } else {
            Fruits fruits = newFruits(number);
            fs.addFruits(fruits);
        }
    }

    //管理员注册账号
    private void register() {
        while (true) {
            System.out.println("请输入要注册的用户名：（不超过8位，可以是大小写字母，数字）");
            String userName = sc.next();

            //判断数据库中是否已存在该用户名
            ArrayList<Managers> managers = ms.getManagers();
            boolean flag = false;
            for (Managers manager : managers) {
                if (manager.getUseName().equals(userName)) {
                    flag = true;
                    break;
                }
            }
            if (flag) {
                System.out.println("该用户名已存在，请重新输入！");
                continue;
            }

            //判断用户名是否输入规范
            boolean judge1 = judge(userName);
            if (!judge1) {
                System.out.println("用户名输入有误，请重新输入！");
                continue;
            }

            //判断密码是否输入规范
            String passWord;
            boolean judge2;
            while (true) {
                System.out.println("请输入密码：（不超过8位，可以是大小写字母，数字）");
                passWord = sc.next();
                judge2 = judge(passWord);
                if (!judge2) {
                    System.out.println("密码输入有误，请重新输入！");
                } else {
                    break;
                }
            }
            ms.addRegister(userName, passWord);
            System.out.println("注册成功！请重新登陆！");
            return;
        }
    }

    //判断用户名和密码的正确性
    private boolean judge(String s) {
        boolean flag = true;

        //判断字符串长度是否小于等于8
        if (s.length() > 8) {
            flag = false;
        } else {
            //判断字符串是否是数字或者字母
            char[] chars = s.toCharArray();
            for (int i = 0; i < chars.length; i++) {
                boolean result = (chars[i] >= 'a' && chars[i] <= 'z') ||
                        (chars[i] >= 'A' && chars[i] <= 'Z') ||
                        (chars[i] >= '0' && chars[i] <= '9');
                if (!result) {
                    flag = false;
                    break;
                }
            }
        }
        return flag;
    }

    //账号登陆
    private void login() {
        ArrayList<Managers> managers = ms.getManagers();//需要判断是否为空，因为文本已写好，这里不做判断
        boolean flag = false;
        System.out.println("请输入账号：");
        String username = sc.next();
        System.out.println("请输入密码：");
        String password = sc.next();
        for (Managers manager : managers) {
            if (username.equals(manager.getUseName()) && password.equals(manager.getPassWord())) {
                flag = true;
                break;
            }
        }

        if (flag) {
            System.out.println("登陆成功！");
            //登陆成功后，调用水果库管理方法
            manage();
        } else {
            System.out.println("账号或密码错误！请重新输入！");
        }
    }

    //封装水果对象
    private Fruits newFruits(String number) {
        System.out.println("请输入水果名称：");
        String name = sc.next();
        System.out.println("请输入水果单价：");
        String price = sc.next();
        System.out.println("请输入水果单位：");
        String unit = sc.next();

        Fruits fruits = new Fruits(number, name, price, unit);
        return fruits;
    }
}
